﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace Common
{
    public class SendMail
    {
        /// <summary>
        /// 发送邮件 WebConfig需配置AppSeting 参数SMTP，EMAIL，EPASS
        /// </summary>
        /// <param name="SendTo">收件人，多个用逗号隔开</param>
        /// <param name="MailTitle">邮件标题</param>
        /// <param name="MailBody">邮件内容，自行Server.HtmlEncode处理</param>
        /// <returns></returns>
        public static bool SendUserMail(string SendTo, string MailTitle, string MailBody)
        {
            bool strResult = false;
            try
            {
                string smtp = Common.GetMes.GetConfigAppValue("SMTP");
                string from = Common.GetMes.GetConfigAppValue("EMAIL");
                string fromPass = Common.GetMes.GetConfigAppValue("EPASS");
                System.Net.Mail.SmtpClient client = new SmtpClient(smtp);
                client.Credentials = new System.Net.NetworkCredential(from, fromPass);
                client.Timeout = 10000;
                client.EnableSsl = false;

                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, SendTo, MailTitle, MailBody);
                message.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
                message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                message.IsBodyHtml = true;
                client.Send(message);
                strResult = true;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return strResult;
        }
    }
}
